// pages/my/my.js
const db=wx.cloud.database().collection('login')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tip:false,
    _id:'',
    edit:false,
    head_src:'/img/headcut.png'
  },
  changepwd:function(){
    const that=this
    wx.navigateTo({
      url: '/pages/changepwd/changepwd?head_src='+that.data.head_src,
    })
  },
  toperson:function(){
    const that=this
    wx.navigateTo({
      url: '/pages/information/information?head_src='+that.data.head_src,
    })
  },
  tip: function (str) {
    const that = this
    that.setData({
      tip_content: str,
      tip: true
    })
    setTimeout(function () {
      that.setData({
        tip: false
      })
    }, 1000)
  },
  changehead:function(){
    const user=wx.getStorageSync('user')
    const that=this
    wx.chooseImage({
      count: 1,
      success(res){
        console.log(res.tempFilePaths[0])
        that.setData({
          head_src:res.tempFilePaths[0]
        })
        console.log(that.data.head_src)
        wx.cloud.uploadFile({
          cloudPath:'leaveapplet/'+user,
          filePath:res.tempFilePaths[0],
          success(res){
            console.log(res)
            db.doc(that.data._id).update({
              data:{
                fileID:res.fileID
              },
              success:function(res){
                that.tip('头像上传成功！')
              },
              error:function(err){
                that.tip('头像上传失败')
              }
              
            })
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const user=wx.getStorageSync('user')
    const that=this
    db.where({
      stuno:user
    }).get({
      success:function(res){
        console.log(res)
        that.setData({
          _id:res.data[0]._id
        })
        wx.cloud.getTempFileURL({
          fileList:[res.data[0].fileID],
          success(res){
            that.setData({
              head_src:res.fileList[0].tempFileURL
            })
            
          }
      })
      }
    })
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (typeof this.getTabBar === 'function' &&
    this.getTabBar()) {
    this.getTabBar().setData({
      selected: 1
    })
  }
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})